from dateutil.utils import today
from datetime import datetime,timedelta
import cnlunar

from App.MyLib.douBao import Doubao


# 生成未来日期的农历信息去去去
def cnlunar_days(num):
    num = int(num)
    current_date = datetime.now()- timedelta(days=  1)
    day_str= {i: {"cnlunar":cnlunar_day(current_date + timedelta(days=i + 1)),"mtime":(current_date + timedelta(days=i + 1)) .timestamp()} for i in range(num)}
    return day_str

# 获取视频文本
def get_video_text(dateArr, db_base_url, db_api_key, init_prompt):
    doubaoVS = Doubao(db_base_url, db_api_key, init_prompt)
    def process_day(day):
        day = day.replace(" ", "")
        strDate = datetime.strptime(day, "%Y-%m-%d")
        strDate_info = cnlunar_day(strDate)
        prompts = doubaoVS.generate_text(strDate_info)
        return {"day": day, "cnlunar_str": strDate_info, "video_scripts": prompts}
    cnlunar_str = list(map(process_day, dateArr))
    return cnlunar_str

def cnlunar_day ( 日期):
    text = []


    # 检查日期类型
    if not isinstance(日期, datetime):
        error_msg = f"时间类型不正确: 需要datetime对象，但收到了{type(日期)}"
        print(error_msg)
        raise TypeError(error_msg)

    hl = cnlunar.Lunar(日期, godType='8char')

    mdic = {
        '月份': hl.date.month,
        "日期": hl.date.day,
        '农历月': hl.lunarMonthCn[0:2],
        '农历日': hl.lunarDayCn,
        '星期': hl.weekDayCn,
        '生肖冲煞': hl.chineseZodiacClash,
        '值日': hl.get_today12DayOfficer()[0],
        '值神': hl.get_today12DayOfficer()[1]
    }
    replace_dict = {
        '廿': '二十',
        '卅': '三十'
    }
    for old_str, new_str in replace_dict.items():
        mdic["农历日"] = mdic["农历日"].replace(old_str, new_str)
    for key, value in mdic.items():
        text.append(f"{key}: {value}")
    return ', '.join(text)



# if __name__ == "__main__":
#     # 示例：如何使用单例模式的 App
#     pass
#
